#include <stdio.h>
#include "standard.h"
#include "utf8.h"

int main(int argc, char** argv)
{
  if (argc < 2) {
    fprintf(stderr, "ERROR: Specify a filename on the commandline.\n");
    return 1;
  }

  FILE* f = fopen(argv[1], "r");
  if (f == NULL) {
    fprintf(stderr, "ERROR: Cannot open filename '%s'.\n", argv[1]);
    return 1;
  }

  /* Read just first 512 bytes */
  uint8_t buffer[512];
  int read_n;
  read_n = fread(buffer, 1, 512, f);
  if (ferror(f)) {
    fprintf(stderr, "ERROR: Error reading '%s'.\n", argv[1]);
    fclose(f);
    return 1;
  }
  uint8_t* p = buffer;
  uni_char c;
  int error;
  while (p < buffer + read_n){
    error = r_utf8_read_uni_char(&p, buffer + read_n, &c);
    fprintf(stdout, "Unicode: %d Error: %d\n", c, error);
  }
  fclose(f);
  return 0;
}
